package com.changgou.consume.service.impl;

import com.changgou.consume.dao.SeckillGoodsMapper;
import com.changgou.consume.dao.SeckillOrderMapper;
import com.changgou.consume.service.SeckillOrderService;
import com.changgou.seckill.pojo.SeckillOrder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author kfzhao
 * @version 1.0
 * @className SeckillOrderServiceImpl
 * @description
 * @date 2022/1/7 0:28
 */
@Service
public class SeckillOrderServiceImpl implements SeckillOrderService {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private SeckillGoodsMapper seckillGoodsMapper;

    @Autowired
    private SeckillOrderMapper seckillOrderMapper;

    /**
     * 同步mysql中的数据
     * @param order
     * @return int
     * @throws
     * @description
     * @author kfzhao
     * @date 2022/1/7 0:36
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int createOrder(SeckillOrder order) {
        // 1.扣减秒杀商品库存
        int result = seckillGoodsMapper.updateStockCount(order.getSeckillId());
        if (result <= 0) {
            // 更新失败
            return 0;
        }
        // 2.新增秒杀订单
        result = seckillOrderMapper.insertSelective(order);
        if (result <= 0) {
            return 0;
        }
        return 1;
    }
}
